package com.seafood.repository;

import com.seafood.entity.ColdChainRecord;
import com.seafood.entity.TemperatureLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 温度记录Repository
 */
@Repository
public interface TemperatureLogRepository extends JpaRepository<TemperatureLog, Long> {
    
    /**
     * 根据冷链记录查找温度日志
     */
    List<TemperatureLog> findByColdChainRecord(ColdChainRecord coldChainRecord);
    
    /**
     * 根据冷链记录ID查找温度日志，按时间倒序
     */
    List<TemperatureLog> findByColdChainRecordIdOrderByRecordTimeDesc(Long coldChainRecordId);
    
    /**
     * 查找最近的温度记录
     */
    @Query("SELECT t FROM TemperatureLog t WHERE t.coldChainRecord.id = :recordId ORDER BY t.recordTime DESC")
    Page<TemperatureLog> findRecentLogs(@Param("recordId") Long recordId, Pageable pageable);
    
    /**
     * 查找异常温度记录
     */
    List<TemperatureLog> findByColdChainRecordAndIsAbnormalTrue(ColdChainRecord coldChainRecord);
    
    /**
     * 根据温度范围查找记录
     */
    @Query("SELECT t FROM TemperatureLog t WHERE t.coldChainRecord.id = :recordId " +
           "AND t.temperature >= :minTemp AND t.temperature <= :maxTemp")
    List<TemperatureLog> findByTemperatureRange(@Param("recordId") Long recordId,
                                               @Param("minTemp") BigDecimal minTemp,
                                               @Param("maxTemp") BigDecimal maxTemp);
    
    /**
     * 根据时间范围查找温度记录
     */
    @Query("SELECT t FROM TemperatureLog t WHERE t.coldChainRecord.id = :recordId " +
           "AND t.recordTime >= :startTime AND t.recordTime <= :endTime")
    List<TemperatureLog> findByTimeRange(@Param("recordId") Long recordId,
                                        @Param("startTime") LocalDateTime startTime,
                                        @Param("endTime") LocalDateTime endTime);
    
    /**
     * 统计异常温度记录数量
     */
    @Query("SELECT COUNT(t) FROM TemperatureLog t WHERE t.coldChainRecord.id = :recordId AND t.isAbnormal = true")
    Long countAbnormalLogs(@Param("recordId") Long recordId);
    
    /**
     * 查找设备离线的记录 - 使用Query注解以避免属性识别问题
     */
    @Query("SELECT t FROM TemperatureLog t WHERE t.coldChainRecord = :coldChainRecord AND t.deviceOnline = false")
    List<TemperatureLog> findByColdChainRecordAndDeviceOnlineFalse(@Param("coldChainRecord") ColdChainRecord coldChainRecord);
    
    /**
     * 获取温度统计信息
     */
    @Query("SELECT MIN(t.temperature), MAX(t.temperature), AVG(t.temperature) " +
           "FROM TemperatureLog t WHERE t.coldChainRecord.id = :recordId")
    Object[] getTemperatureStatistics(@Param("recordId") Long recordId);
    
    /**
     * 获取湿度统计信息
     */
    @Query("SELECT MIN(t.humidity), MAX(t.humidity), AVG(t.humidity) " +
           "FROM TemperatureLog t WHERE t.coldChainRecord.id = :recordId")
    Object[] getHumidityStatistics(@Param("recordId") Long recordId);
    
    /**
     * 删除指定时间之前的旧记录
     */
    @Query("DELETE FROM TemperatureLog t WHERE t.recordTime < :cutoffTime")
    void deleteOldLogs(@Param("cutoffTime") LocalDateTime cutoffTime);
}